#ifndef B2D_STRINGID_H
#define B2D_STRINGID_H

#include "Prerequisites.h"

NS_B2D_BEGIN

////////////////////////////////////////////////////////////////////////////////////////////////////
/// \brief	StringId stores a string along with its hash. This enabled to check hashes match
/// 		before comparing the whole string.
////////////////////////////////////////////////////////////////////////////////////////////////////
class B2D_API StringId final
{
public:
	typedef size_t Hash;

private:
	Hash mHash;
	string mString;

public:
	StringId(void) : mHash(hash_function(string(""))), mString("") { }
	StringId(const StringId& id) : mHash(id.mHash), mString(id.mString) { }
	StringId(const string& s) : mHash(hash_function(s)), mString(s) { }
	StringId(const char* s) : mHash(hash_function(s)), mString(s) { }

	// Asignment operators
	StringId& operator = (const StringId& id)
	{
		mHash = id.mHash;
		mString = id.mString;
		return *this;
	}
	StringId& operator = (const string& s)
	{
		mHash = hash_function(s);
		mString = s;
		return *this;
	}

	// Comparasion operators
	bool operator == (const StringId& id) const { return mHash == id.mHash && mString == id.mString; }
	bool operator != (const StringId& id) const { return mHash != id.mHash && mString != id.mString; }
	bool operator == (const string& s) const { return mString == s; }
	bool operator != (const string& s) const { return mString != s; }
	bool operator == (const char* s) const { return mString.compare(s) == 0; }
	bool operator != (const char* s) const { return mString.compare(s) != 0; }

	// Convert to Hash
	FORCE_INLINE operator Hash() const { return mHash; }
	FORCE_INLINE Hash getHash(void) const { return mHash; }
	// Convert to string
	FORCE_INLINE operator string() const { return mString; }
	FORCE_INLINE const string& getString(void) const { return mString; }
};

NS_B2D_END

namespace std
{
	template<> struct hash<::b2d::StringId>
	{
		size_t operator()(const ::b2d::StringId& id)
		{
			return (size_t)id;
		}
	};
}

#endif